<?	 

class Article extends Action{	

	private $cacheDir='article';//缓存目录
	
	
//查询数据列表
	public function execute(){

		empty($_GET['page'])?$currentPage=1:$currentPage= $_GET['page'];
		$page_size=2;
		$countSql='select id from tb_demo ';
		$totalNum=$this->C($this->cacheDir)->countRecords($countSql);
		$page = _instance(EXTEND.'JPage',array($totalNum, $page_size, $currentPage,  ACT.'/Index/execute/page/'));//初始化框架扩展中分页类
			 	
		$sql=" select * from tb_demo  order by id desc limit ".($currentPage-1)*$page_size.",$page_size";	
		$list=$this->C($this->cacheDir)->findAll($sql);
		//查询方法：findAll、findOne、countRecords,在查询时第二个参数可以传递缓存时间以实现局部缓存时间设置如：findAll($sql,60)
		$assignArray=array('articleList'=>$list,'page'=>$page->showPage());//适合多个变量注入	
		return $assignArray;		 
	}

//查询一条记录	
	public function findOne(){
	
		$this->assign($this->getOne());
		$this->show('articleOne');				
	}

//添加记录		
	public function add (){
		
		if(empty($_POST)){
			$this->show('articleAdd');     		      
		}else{
			
			$createTime=date("Y-m-j-H:i:s",time());
			$sql="insert into tb_demo (title,content,time) values ('$_POST[title]','$_POST[content]','$createTime')";					
			if($this->C($this->cacheDir)->insert($sql)){//返回新增记录ID
			$this->location('Index');
			}
		}		
	}	

//修改记录	
	public function update (){
	 
		if(empty($_POST)){

			$this->assign($this->getOne());//调用方法，并注入到页面中
			$this->show('articleUpdate');		
		}else{		
			$sql="update tb_demo set title= '$_POST[title]',content= '$_POST[content]' where id=".$_GET['id'];				
			if($this->C($this->cacheDir , $_GET['id'])->update($sql)>=0){//需传入缓存唯一标识，即该记录的ID，框架将更新此记录的缓存文件		
			//返回影响记录数大于等于零表示：大于零时内容已被修改，等于零时内容没有修改但用户有提交更新操作动作
			$this->location('Index');
			}			 
		}			
	}


//删除记录
	public function del (){

		$sql="delete from tb_demo where id=".$_GET['id'];											 
		if($this->C($this->cacheDir ,$_GET['id'])->delete($sql)>0){//传入缓存唯一标识，即该记录的ID，框架会自动将此缓存文件也删除
		$this->location('Index');
		}else{
		echo '删除失败';//返回影响记录数等于零表示删除失败
		}
	}
	


//查询一条记录公共方法
	public function getOne(){
	
		$sql="select * from tb_demo where id=".$_GET['id'];				 
		$articleOne= $this->C($this->cacheDir, $_GET['id'])->findOne($sql);//需传入缓存唯一标识，即该记录的ID
		$assingArray=array('articleOne'=>$articleOne);
		return $assingArray;		
				
	}	


}//
